package cn.com.twoke.application.game.mario.game.entity;

import cn.com.twoke.application.game.mario.core.ecs.world.GameWorld;
import cn.com.twoke.application.game.mario.core.ecs.component.ColliderComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.PositionComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.RigidbodyComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.SizeComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.texture.TextureRenderComponent;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import cn.com.twoke.application.game.mario.game.component.RenderFillComponent;
import cn.com.twoke.application.game.mario.game.config.Tiles;
import cn.hutool.core.lang.Opt;

import java.awt.*;

public class BlockBuilder extends AbstractEntityBuilder<BlockBuilder> {

    private EntityType type = EntityType.GROUND;
    private int tileRows = 1;
    private int tileCols = 1;
    private String textureName =  "normal-block";
    private RenderFillComponent fillComponent;

    public BlockBuilder(GameWorld world) {
        super(world);
    }


    public BlockBuilder type(EntityType type) {
        this.type = type;
        return this;
    }

    public BlockBuilder tileRows(int tileRows) {
        this.tileRows = tileRows;
        return this;
    }

    public BlockBuilder tileCols(int tileCols) {
        this.tileCols = tileCols;
        return this;
    }

    public BlockBuilder textureName(String textureName) {
        this.textureName = textureName;
        return this;
    }

    public BlockBuilder fillComponent(RenderFillComponent fillComponent) {
        this.fillComponent = fillComponent;
        return this;
    }


    @Override
    public Entity build() {
        Entity entity = world.createEntity();
        // 实体类型
        entity.setType(type);
        Tiles titles = Tiles.of(tileCols, tileRows);
        PositionComponent positionComponent = new PositionComponent(startX, startY);
        SizeComponent sizeComponent = SizeComponent.builder()
                .width(titles.getWidth())
                .height(titles.getHeight())
                .build();

        ColliderComponent colliderComponent = new ColliderComponent(() -> new Rectangle(
                (int) positionComponent.x,
                (int) positionComponent.y,
                titles.getWidth(),
                titles.getHeight()
        ));
        RigidbodyComponent rigidbodyComponent = new RigidbodyComponent();
        rigidbodyComponent.bodyType = RigidbodyComponent.BodyType.STATIC;
        entity.addComponent(positionComponent); // 位置组件
        entity.addComponent(sizeComponent); // 尺寸组件
        entity.addComponent(colliderComponent); // 碰撞体组件
        entity.addComponent(rigidbodyComponent); // 刚体组件
        entity.addComponent(TextureRenderComponent.builder()
                .textureId(() -> textureName)
                .build());

        Opt.ofNullable(fillComponent).ifPresent(fillComponent -> entity.addComponent(fillComponent));
        // 扩展操作
        Opt.ofNullable(extend).ifPresent(extend -> extend.accept(entity));

        return entity;
    }


}
